import Vue from 'vue'
import VueRouter from 'vue-router'
import store from "@/store";

Vue.use(VueRouter)

const routes = [
    {
        path: '/login',
        name: 'Login',
        component: () => import('../views/Login.vue')
    },
    {
        path: '/register',
        name: 'Register',
        component: () => import('../views/Register.vue')
    },
    {
        path: '/404',
        name: '404',
        component: () => import('../views/404.vue')
    },
    {
        path: '/exam/do',
        name: 'Do',
        component: () => import('../views/Do.vue')
    },
    // {
    //     path: '/paper/checkPaper',
    //     name: '批改试卷',
    //     component: () => import('../views/CheckPaper.vue')
    // },
    {
        path: '/paper/check',
        name: 'Check',
        component: () => import('../views/Check.vue')
    },
    {
        path: '/exam/result',
        name: 'Result',
        component: () => import('../views/Result.vue')
    },
    {
        path: '/exam/checkResult',
        name: 'CheckResult',
        component: () => import('../views/CheckResult.vue')
    },

]



const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})

export const resetRouter = () => {
    router.matcher = new VueRouter({
        mode: 'history',
        base: process.env.BASE_URL,
        routes
    })
}

// 注意：刷新页面会导致页面路由重置
export const setRoutes = () => {
    const storeMenus = localStorage.getItem("menus");
    if (storeMenus) {

        // 获取当前的路由对象名称数组
        const currentRouteNames = router.getRoutes().map(v => v.name)
        if (!currentRouteNames.includes('Layout')) {
            // 拼装动态路由
            const manageRoute = { path: '/', name: '主页',  component: () => import('../layout/Layout.vue'), children: [
                    { path: 'home', name: '主页', component: () => import('../views/Home.vue')},
                    { path: 'password', name: '修改密码', component: () => import('../views/Password.vue')},
                ] }
            const menus = JSON.parse(storeMenus)
            menus.forEach(item => {
                if (item.path) {  // 当且仅当path不为空的时候才去设置路由
                    let itemMenu = { path: item.path.replace("/", ""), name: item.name, component: () => import('../views/' + item.pagePath + '.vue')}
                    manageRoute.children.push(itemMenu)
                } else if(item.children.length) {
                    item.children.forEach(item => {
                        if (item.path) {
                            let itemMenu = { path: item.path.replace("/", ""), name: item.name, component: () => import('../views/' + item.pagePath + '.vue')}
                            manageRoute.children.push(itemMenu)
                        }
                    })
                }
            })
            // 动态添加到现在的路由对象中去
            router.addRoute(manageRoute)
        }

    }
}

// 重置我就再set一次路由
setRoutes()


router.beforeEach((to, from, next) => {
    localStorage.setItem("currentPathName", to.name)  // 设置当前的路由名称
    localStorage.setItem("path", to.path)  // 设置当前的路由路径
    store.commit("setPath")

    // 未找到路由的情况
    if (!to.matched.length) {
        const storeMenus = localStorage.getItem("menus")
        if (storeMenus) {
            if(to.name != "Password"){
                next("/404")
            }
        } else {
            // 跳回登录页面
            next("/login")
        }
    }
    // 其他的情况都放行
    next()
})

export default router
